package com.lb.shopguide.ui.fragment.guide.pay;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.order.CommitOrderReplyBean;
import com.lb.shopguide.event.guide.SwitchPayTypeEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by kang on 2017/6/9.
 * 支付订单
 */

public class FragmentPayOrder extends SupportFragment {
    private Context mContext;
    private View view;

    private CommitOrderReplyBean mReplyBean;
    private String mMemberCode;

    public static FragmentPayOrder newInstance(CommitOrderReplyBean replyBean, String memberCode) {
        FragmentPayOrder fragmentPayOrder = new FragmentPayOrder();
        Bundle bundle = new Bundle();
        bundle.putSerializable(AppConstant.COMMIT_REPLY_BEAN, replyBean);
        bundle.putString(AppConstant.MEMBER_CODE, memberCode);
        fragmentPayOrder.setArguments(bundle);
        return fragmentPayOrder;
    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_pay_order, container, false);
        EventBus.getDefault().register(this);
        initData();
        initView(savedInstanceState);
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    private void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.COMMIT_REPLY_BEAN)) {
            mReplyBean = (CommitOrderReplyBean) bundle.getSerializable(AppConstant.COMMIT_REPLY_BEAN);
        }
        if (bundle.containsKey(AppConstant.MEMBER_CODE)) {
            mMemberCode = bundle.getString(AppConstant.MEMBER_CODE);
        }
    }

    public void initView(@Nullable Bundle savedInstanceState) {
        if (savedInstanceState == null) {
            switch (Integer.parseInt(mReplyBean.getPayMode())) {
                case AppConstant.PAY_TYPE_ALIPAY:
                    loadRootFragment(R.id.fl_container, FragmentPayAlipay.newInstance(mReplyBean, mMemberCode));
                    break;
                case AppConstant.PAY_TYPE_WECHAT_PAY:
                    loadRootFragment(R.id.fl_container, FragmentPayWechat.newInstance(mReplyBean, mMemberCode));
                    break;
                case AppConstant.PAY_TYPE_CASH_PAY:
                    loadRootFragment(R.id.fl_container, FragmentPayCash.newInstance(mReplyBean, mMemberCode));
                    break;
                case AppConstant.PAY_TYPE_BALANCE_PAY:
                    loadRootFragment(R.id.fl_container, FragmentPayBalance.newInstance(mReplyBean, mMemberCode));
                    break;
            }

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onChangePayOrder(SwitchPayTypeEvent event) {
        CommitOrderReplyBean replyBean = event.getReplyBean();
        switch (Integer.parseInt(replyBean.getPayMode())) {
            case AppConstant.PAY_TYPE_ALIPAY:
                loadRootFragment(R.id.fl_container, FragmentPayAlipay.newInstance(mReplyBean, mMemberCode), false, true);
                break;
            case AppConstant.PAY_TYPE_WECHAT_PAY:
                loadRootFragment(R.id.fl_container, FragmentPayWechat.newInstance(mReplyBean, mMemberCode), false, true);
                break;
            case AppConstant.PAY_TYPE_CASH_PAY:
                loadRootFragment(R.id.fl_container, FragmentPayCash.newInstance(mReplyBean, mMemberCode), false, true);
                break;
            case AppConstant.PAY_TYPE_BALANCE_PAY:
                loadRootFragment(R.id.fl_container, FragmentPayBalance.newInstance(mReplyBean, mMemberCode), false, true);
                break;
        }
    }

}
